﻿using Dapper;
using System.Text;
using KL.Standard.Domain.Repositories;
using KL.Standard.Model.Models.BaseArch;
using KL.Standard.Repositories.MutliDataSource;

namespace KL.Standard.Repositories.BaseArch
{
    public class AttachRepository : BaseRepository<SysAttachmentPO, ECPDataSource>, IAttachRepository
    {
        public Task<int> AddAttachment(List<SysAttachmentPO> infos)
        {
            string vSql = string.Format(@"
                INSERT INTO [dbo].[sys_attachment]
                       ([uID]
                       ,[mainID]
                       ,[dataTable]
                       ,[attachType]
                       ,[fileName],[realFileName]
                       ,[fileSuffix]
                       ,[webUrl]
                       ,[phyAddress]
                       ,[createdTime]
                       ,[creatorID],[bldBatchID]
                       ,isSyn,SynDt,bucketName,objectName)
                 VALUES
                       (newID()
                       ,@mainID
                       ,@dataTable
                       ,@attachType
                       ,@fileName,@realFileName
                       ,@fileSuffix
                       ,@webUrl
                       ,@phyAddress
                       ,@createdTime
                       ,@creatorID,@bldBatchID
                       ,@isSyn,@synDt,@bucketName,@objectName  )
            ");
            return this.DbConn.ExecuteAsync(vSql, infos);
        }

        public int DeleteAttachment(string uID)
        {
            string vSql = string.Format(@"delete from sys_attachment where uID= @uID");
            return this.DbConn.Execute(vSql, new {  uID });
        }

        public List<SysAttachmentPO> GetAllAttachments(Dictionary<string, string> searchInfo)
        {
            var vSql = new StringBuilder(@" SELECT cast([uID] as varchar(64)) as uID
                , cast([mainID] as varchar(64)) as mainID
                ,[dataTable]
                ,[attachType]
                ,[fileName]
                ,[realFileName]
                ,[fileSuffix]
                ,[webUrl]
                ,[phyAddress]
                ,[createdTime]
                ,[creatorID] AS bldID
            FROM[dbo].[sys_attachment]  where 1=1 ");
            DynamicParameters dp = new DynamicParameters();
            if (searchInfo.ContainsKey("mainID"))
            {
                vSql.Append($" and mainID = @mainID ");
                dp.Add("mainID", searchInfo["mainID"]);
            }
            if (searchInfo.ContainsKey("attachType"))
            {
                vSql.Append($" and attachType = @attachType ");
                dp.Add("attachType", searchInfo["attachType"]);
            }
            if (searchInfo.ContainsKey("dataTable"))
            {
                vSql.Append($" and dataTable = @dataTable ");
                dp.Add("dataTable", searchInfo["dataTable"]);
            }


            var list = this.DbConn.Query<SysAttachmentPO>(vSql.ToString(), dp).ToList();

            return list;
        }

        public Task<int> DeleteAttachment4MainID(string uID)
        {
            string vSql = @"delete from sys_attachment where mainID=@mainID";
            return this.DbConn.ExecuteAsync(vSql, new { mainID = uID });
        }

        public Task<IEnumerable<SysAttachmentPO>> GetRecordsFromBatchId(string? bldBatchId)
        {
            return this.GetListAsync<SysAttachmentPO>(tableName: "sys_attachment", whereSql: "bldBatchId = @bldBatchId",
                orderSql: " createdTime ASC ",param:new { bldBatchId });
        }

        public async Task<bool> UpSynFlag(SysAttachmentPO attachment)
        {
            var vSql = $@"
                update SYS_ATTACHMENT SET 
                    isSyn = 1 ,synDt = getdate(),bucketName=@bucketName,objectName = @objectName 
                where uID = @uID ;";
            var count = await this.DbConn.ExecuteAsync(vSql, attachment);
            return count > 0;
        }

        public Task<SysAttachmentPO> GetDetailsForId(string uId)
        {
            return this.GetFirstOrDefaultAsync<SysAttachmentPO>(tableName: null, whereSql: " uID = @uId ",
                param: new { uId });
        }
    }
}
